﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;




namespace HomeWork
{

    class Problem : Content
    {
        //public User publisher; //悬赏发布者
        public User Solver { get; set; }    //悬赏解决者
        public User Responder { get; set; } //悬赏响应者
        public HelpMoney HelpMoney { get; set; }//帮帮币
        public String Summarize { get; set; } //问题总结
        public IList<Keyword> Keywords { get; set; } //关键字
        public int State { get; set; } //状态
        public IList<Comment> comments { get; set; }//一对多，评论

        private string[] _keywords = { "SQL", "C#", "JavaScript", "php", "c++", "java", "asp", "python", "nosql", "css" };
        public string this[int index]
        {
            get
            {
                ValidateIndex(index); // 边界检查
                return _keywords[index];
            }
            set
            {
                ValidateIndex(index); // 边界检查
                _keywords[index] = value;
            }
        }
        private int _reward = 10;//显式私有字段Reward
        public int Reward //悬赏金额
        {
            get => _reward;
            set
            {
                if (value < 0)
                {
                    //Console.WriteLine("悬赏金额不能小于0");
                    throw new ArgumentOutOfRangeException
                    (
                        paramName: nameof(Reward),  // 参数名：明确是 Reward 字段
                        actualValue: value,        // 实际值：方便后续获取负数
                        message: "悬赏金额不能小于0"
                    );
                }//else nothing
                _reward = value;
                Console.WriteLine("设置了悬赏金额：" + Reward);


            }
        }

        // 边界验证方法
        private void ValidateIndex(int index)
        {
            if (index < 0 || index >= _keywords.Length)
                throw new IndexOutOfRangeException($"索引 {index} 越界，有效范围：[0, {_keywords.Length - 1}]");
        }

        public Problem()
        {
            HelpMoney = new HelpMoney(); //初始化帮帮币

        }

        //public Problem(int id) : base(id)
        //{

        //}

        //设计多个重载的Publish方法，满足不同的调用需求
        public void Publish(string title, string body, int reward)
        {
            Reward = reward; // 利用属性的验证逻辑
            OutputPublishInfo(title, body, Reward);
        }

        public void Publish(string title, string body)
        {
            Publish(title, body, 10);
        }

        public void Publish(string title, int reward)
        {
            Publish(title, title, reward);

        }

        public void Publish(string title)
        {
            Publish(title, title, 10);
        }

        private void OutputPublishInfo(string title, string body, int reward)
        {
            Console.WriteLine($"发布文章：{title}");
            Console.WriteLine($"内容为：{body}");
            Console.WriteLine($"设置悬赏：{reward}枚帮帮币");
        }

        //发行文章
        //public override void Release()
        //{
        //    Console.WriteLine("准备发布problem");
        //    HelpMoney.DisableAmount = Reward;//根据悬赏金额设置冻结金额
        //    HelpMoney.AncestorDecrease();//扣费
        //    Console.WriteLine("保存文章到数据库");
        //}

        //发布文章
        public override void Publish()
        {
            ValidateAuthorNotNull(); //验证作者不为空
            Reward=-110;
            HelpMoney.DisableAmount = Reward; //根据悬赏金额设置冻结金额
            HelpMoney.Decrease(); //扣费
            if (Reward > 0)
            {
                Console.WriteLine("发布了一个问题：");
            }
            Console.WriteLine("消耗了" + Reward + "枚帮帮币");
        }

        public static void Load(int id)
        {
            Console.WriteLine("加载求助：" + id);
        }

        public static void Delete(int id)
        {
            Console.WriteLine("删除求助" + id);
        }


    }
}
